<template>
  <div>
    <a-row>
      <a-col :span="24" style="margin-bottom: 15px;">
        <a-button-group>
          <a-button type="primary">添加</a-button>
          <a-button type="primary">编辑</a-button>
          <a-button type="primary">删除</a-button>
          <a-button type="primary" @click="handlerAuthor">权限分配</a-button>
          <a-button type="primary" @click="handlerUser">关联用户</a-button>
        </a-button-group>
      </a-col>
      <a-col :span="10">
        <a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onChange" />
        <a-tree
          :expanded-keys="expandedKeys"
          :auto-expand-parent="autoExpandParent"
          :tree-data="gData"
          @expand="onExpand"
        >
          <template slot="title" slot-scope="{ title }">
            <span v-if="title.indexOf(searchValue) > -1">
              {{ title.substr(0, title.indexOf(searchValue)) }}
              <span style="color: #f50">{{ searchValue }}</span>
              {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
            </span>
            <span v-else>{{ title }}</span>
          </template>
        </a-tree>
      </a-col>
      <a-col :span="14">
        <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
            <a-form-item label="名称">
              <a-input
                v-decorator="['name', { rules: [{ required: true, message: 'Please input your note!' }] }]"
              />
            </a-form-item>
            <a-form-item label="编码">
              <a-input
                v-decorator="['code', { rules: [{ required: true, message: 'Please input your note!' }] }]"
              />
            </a-form-item>
            <a-form-item label="描述">
              <a-input
                v-decorator="['desc', { rules: [{ required: true, message: 'Please input your note!' }] }]"
              />
            </a-form-item>
            <a-form-item v-if="formStatus == 'update'">
              <a-button type="primary" v-if="groupManager_btn_edit" @click="update">更新</a-button>
              <a-button @click="onCancel">取消</a-button>
            </a-form-item>
            <a-form-item v-if="formStatus == 'create'">
              <a-button type="primary" v-if="groupManager_btn_add" @click="create">保存</a-button>
              <a-button @click="onCancel">取消</a-button>
            </a-form-item>
          </a-form>
      </a-col>
      <a-modal
        title="关联用户"
        :visible="visibleUser"
        :confirm-loading="confirmLoading"
        @ok="handleOkUser"
        @cancel="handleCancelUser"
        width="500px"
      >
        <group-user :groupId="groupId"></group-user>
      </a-modal>
      <a-modal
        title="权限分配"
        :visible="visibleAuthor"
        :confirm-loading="confirmLoadingAuthor"
        @ok="handleOkAuthor"
        @cancel="handleCancelAuthor"
        width="800px"
      >
        <group-authority :groupId="groupId"></group-authority>
      </a-modal>
    </a-row>
  </div>
</template>

<script>
import GroupUser from './GroupUser.vue'
import GroupAuthority from './GroupAuthority.vue'
const x = 3;
const y = 2;
const z = 1;
const gData = [];
const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || '0';
  const tns = _tns || gData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({ title: key, key, scopedSlots: { title: 'title' } });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);

const dataList = [];
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({ key, title: key });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(gData);

const getParentKey = (key, tree) => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some(item => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};
export default {
  name: 'GroupDetail',
  components: {
    GroupUser,
    GroupAuthority
  },
  data () {
    return {
      groupManager_btn_edit: false,
      groupManager_btn_add: false,
      formStatus: '',
      expandedKeys: [],
      searchValue: '',
      autoExpandParent: true,
      gData,
      visibleAuthor: false,
      visibleUser: false,
      confirmLoading: false,
      confirmLoadingAuthor: false,
      form: null,
      groupId: -1
    }
  },
  methods: {
    onSelect (selectedKeys, info) {
      console.log('selected', selectedKeys, info);
    },
    onExpand (expandedKeys) {
          this.expandedKeys = expandedKeys;
          this.autoExpandParent = false;
        },
    onChange (e) {
      const value = e.target.value;
      const expandedKeys = dataList
        .map(item => {
          if (item.title.indexOf(value) > -1) {
            return getParentKey(item.key, gData);
          }
          return null;
        })
        .filter((item, i, self) => item && self.indexOf(item) === i);
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true
      });
    },
    handlerUser () {
      this.visibleUser = true;
    },
    handleOkUser (e) {
      this.ModalText = 'The modal will be closed after two seconds';
      this.confirmLoading = true;
      setTimeout(() => {
        this.visibleUser = false;
        this.confirmLoading = false;
      }, 2000);
    },
    handleCancelUser (e) {
      console.log('Clicked cancel button');
      this.visibleUser = false;
    },
    handlerAuthor () {
      this.visibleAuthor = true;
    },
    handleOkAuthor (e) {
      this.ModalText = 'The modal will be closed after two seconds';
      this.confirmLoadingAuthor = true;
      setTimeout(() => {
        this.visibleAuthor = false;
        this.confirmLoadingAuthor = false;
      }, 2000);
    },
    handleCancelAuthor (e) {
      console.log('Clicked cancel button');
      this.visibleAuthor = false;
    }
  }
}
</script>

<style>
</style>
